<template>
  <div class="mod-user">
    <!-- <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.search" placeholder="关键词" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form> -->
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        prop="userId"
        header-align="center"
        align="center"
        width="80"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        label="用户名">
      </el-table-column>
      <el-table-column
        prop="email"
        header-align="center"
        align="center"
        width="180"
        label="邮箱">
      </el-table-column>
      <el-table-column
        prop="mobile"
        header-align="center"
        align="center"
        label="手机号">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        label="创建时间">
      </el-table-column>
      <el-table-column 
        header-align="center"
        align="center"
         width="120"
        label="权限管理">
          <template slot-scope="scope">
         <el-button
          size="mini"
          type="primary"
          @click="setPermission(scope.row)">修改</el-button>
          </template>
    </el-table-column>
    </el-table>
    <!-- <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination> -->
    
    <el-dialog title="节点权限管理" :visible.sync="dialogVisible">
        <el-table :data="gridData" @selection-change="selectionChangeHandle" style="width: 100%" ref="multipleTable">
            <el-table-column
                type="selection"
                header-align="center"
                align="center"
                width="50">
            </el-table-column>
            <el-table-column
                prop="id"
                header-align="center"
                align="center"
                width="80"
                label="节点ID">
            </el-table-column>
             <el-table-column
                prop="ip"
                header-align="center"
                align="center"
                width="120"
                label="IP">
            </el-table-column>
            <el-table-column
                prop="no"
                width="100"
                header-align="center"
                align="center"
                label="no">
            </el-table-column>
            <el-table-column
                prop="count"
                header-align="center"
                align="center"
                width="100"
                label="出票总数">
            </el-table-column>
            <el-table-column
              prop="selected"
              width="120"
              header-align="center"
              align="center"
              label="是否授权">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.selected"
                  @change="onChage(scope.row)"
                  active-color="#13ce66"
                  inactive-color="#ff4949">
                </el-switch>
              </template>
            </el-table-column>
        </el-table>
        <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next">
        </el-pagination>
        <span class="span_box">
            <el-button type="primary" @click="onAdd">批量授权</el-button>
            <el-button type="danger" @click="onRemove">批量移除</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
/* eslint-disable */ 
  export default {
    data () {
      return {
        gridData: [],
        dialogVisible: false,
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        multipleSelection: [],
        userId:null,
        flag:false
      }
    },
    activated () {
      this.getUserList()
    },
    methods: {
      // 点击修改
      setPermission(row){
        // console.log(row.userId)
        this.pageIndex = 1
        if(row){
          this.userId = row.userId
        }
        this.dialogVisible = true
        this.getNodeList()
      },
        // 获取节点列表
        getNodeList(){
          this.$http({
          url: this.$http.adornUrl('/bzz/getAuthNodeList/'+this.userId),
          method: 'post',
          data: this.$http.adornData({
              'page':this.pageIndex,
              'limit':this.pageSize,
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              console.log(data);
              this.gridData = data.page.list
              this.totalPage = data.page.totalCount
            } else {
              this.gridData = []
              this.totalPage = 0
            }
          })
        },

      // 获取用户权限的用户列表
      getUserList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/bzz/userList'),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getNodeList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getNodeList()
      },
      // 多选
      selectionChangeHandle (rows) {
         this.multipleSelection = [];
          if (rows) {
              rows.forEach(row => {
                  if (row) {
                      this.multipleSelection.push(row.id);
                  }
              });
          }
          console.log('this.multipleSelection');
          console.log(this.multipleSelection);
      },
      save(){
      },
      // 改变是否授权
      onChage(e){
        if(e.selected){
          this.getAuthNode('authNode',[e.id])
        }else{
          this.getAuthNode('removeAuthNode',[e.id])
        }
      },

      //改变是否授权
      getAuthNode(changes,arr){
          console.log(arr)
          this.$http({
          url: this.$http.adornUrl('/bzz/'+ changes +'/'+this.userId),
          method: 'post',
          data:arr
          }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '授权成功',
              type: 'success',
              duration: 1000
            })
            if(this.flag){
              this.getNodeList()
              this.flag = false
            }
          } else {
            this.$message.error(data.msg)
          }
          })
      },
      onAdd(){
        this.flag = true
        if(this.multipleSelection.length > 0){
          this.getAuthNode('authNode',this.multipleSelection)
        }else{
          this.$message({
              message: '请勾选列表',
              type: 'error',
              duration: 1000
            })
        }
      },
      onRemove(){
        this.flag = true
        if(this.multipleSelection.length > 0){
          this.getAuthNode('removeAuthNode',this.multipleSelection)
        }else{
          this.$message({
              message: '请勾选列表',
              type: 'error',
              duration: 1000
            })
        }
      }
    }
      
    }
  
</script>

<style lang="">
    .el-dialog{min-width: 700px;}
    .site-wrapper .el-pagination{text-align: left;}
    .span_box{padding-top: 30px;display: block;}
</style>
